﻿using System;
using System.Collections.Generic;
using System.Linq;
using XRM.ActiveDirectoryService;

namespace XRM.Security.WebUI.Services
{
    public class FakeADService : IActiveDirectoryService
    {
        private static readonly List<ADUser> Users = new List<ADUser>
        {
            new ADUser { FirstName = "Егор", LastName = "Сизый", AccountName = "syz", MiddleName = "Петрович" },
            new ADUser { FirstName = "Игорь", LastName = "Рябой", AccountName = "ryab", MiddleName = "" },
            new ADUser { FirstName = "Михаил", LastName = "Махач", AccountName = "maha", MiddleName = "" },
            new ADUser { FirstName = "Михаил", LastName = "Сикач", AccountName = "syza", MiddleName = "" }
        };

        public bool Validate(string login, string password)
        {
            return true;
        }

        public List<ADUser> Search(string lastName, string firstName)
        {
            IEnumerable<ADUser> result = Users;
            if (lastName != null)
            {
                result = result.Where( s => s.LastName.Equals( lastName, StringComparison.CurrentCultureIgnoreCase ) );
            }
            if ( firstName != null )
            {
                result = result.Where( s => s.FirstName.Equals( firstName, StringComparison.CurrentCultureIgnoreCase ) );
            }
            return result.ToList();
        }

        public List<ADUser> SearchByUserName(string userName)
        {
            return Users.Where( s => s.AccountName.StartsWith(userName.ToLower()) ).ToList();
        }

        public List<ADUser> SearchByFullName(string fullName)
        {
            return Users.Where( s => s.FullName.ToLower().StartsWith( fullName.ToLower() ) ).ToList();
        }

        public int Count(string fullName)
        {
            return SearchByFullName(fullName).Count;
        }
    }
}